package com.hncboy.chatgpt.front.handler.emitter;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hncboy.chatgpt.base.domain.entity.ChatCustomerDO;
import com.hncboy.chatgpt.base.domain.entity.ChatOrderDO;
import com.hncboy.chatgpt.base.mapper.ChatCustomerMapper;
import com.hncboy.chatgpt.base.util.ObjectMapperUtil;
import com.hncboy.chatgpt.front.domain.request.ChatProcessRequest;
import com.hncboy.chatgpt.front.domain.vo.ChatReplyMessageVO;
import com.hncboy.chatgpt.front.mapper.ChatOrderMapper;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import java.io.IOException;
import java.util.List;

/**
 * @author lizhongyuan
 * 限制聊天次数
 */
public class ChatLimiterEmitterChain extends AbstractResponseEmitterChain {

    @Override
    public void doChain(ChatProcessRequest request, ResponseBodyEmitter emitter) {
        try {
            ChatOrderMapper chatOrderMapper = SpringUtil.getBean(ChatOrderMapper.class);
            ChatCustomerMapper chatCustomerMapper = SpringUtil.getBean(ChatCustomerMapper.class);

            QueryWrapper<ChatCustomerDO> queryWrapper = new QueryWrapper();
            queryWrapper.eq("id", request.getCustomerId());
            ChatCustomerDO chatCustomerDO = chatCustomerMapper.selectOne(queryWrapper);
            if (chatCustomerDO != null) {
                Long id = chatCustomerDO.getId();
                QueryWrapper<ChatOrderDO> chatOrderDO = new QueryWrapper();
                chatOrderDO.eq("customer_id", id);
                chatOrderDO.eq("status", 1);
                List<ChatOrderDO> chatOrderDOS = chatOrderMapper.selectList(chatOrderDO);
                Integer limits = chatCustomerDO.getLimits();
                if (CollUtil.isEmpty(chatOrderDOS)) {
                    if (limits > 0) {
                        if (getNext() != null) {
                            getNext().doChain(request, emitter);
                            chatCustomerDO.setLimits(chatCustomerDO.getLimits() - 1);
                            chatCustomerMapper.updateById(chatCustomerDO);
                        }
                    } else {
                        ChatReplyMessageVO chatReplyMessageVO = ChatReplyMessageVO.onEmitterChainException(request);
                        chatReplyMessageVO.setText(StrUtil.format("普通用户一天限制聊天次数：5次"));
                        emitter.send(ObjectMapperUtil.toJson(chatReplyMessageVO));
                        emitter.complete();
                    }
                } else {
                    long end = chatOrderDOS.get(chatOrderDOS.size() - 1).getEnd().getTime();
                    long now = DateUtil.current();
                    long difference = end - now;
                    if (difference > 0) {
                        if (getNext() != null) {
                            getNext().doChain(request, emitter);
                            chatCustomerDO.setLimits((chatCustomerDO.getLimits() - 1) < 0 ? 0 : chatCustomerDO.getLimits() - 1);
                            chatCustomerMapper.updateById(chatCustomerDO);
                        }
                    } else {
                        if (limits > 0) {
                            if (getNext() != null) {
                                getNext().doChain(request, emitter);
                                chatCustomerDO.setLimits((chatCustomerDO.getLimits() - 1) < 0 ? 0 : chatCustomerDO.getLimits() - 1);
                                chatCustomerMapper.updateById(chatCustomerDO);
                            }
                        } else {
                            ChatReplyMessageVO chatReplyMessageVO = ChatReplyMessageVO.onEmitterChainException(request);
                            chatReplyMessageVO.setText(StrUtil.format("当前会员已过期，普通用户一天限制聊天次数：5次"));
                            emitter.send(ObjectMapperUtil.toJson(chatReplyMessageVO));
                            emitter.complete();
                        }
                    }
                }
            } else {
                ChatReplyMessageVO chatReplyMessageVO = ChatReplyMessageVO.onEmitterChainException(request);
                chatReplyMessageVO.setText(StrUtil.format("当前用户无效，请检测账号是否安全！"));
                emitter.send(ObjectMapperUtil.toJson(chatReplyMessageVO));
                emitter.complete();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
